package vnc.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TCPbit extends JPanel{

	public String[] names;
	public Object[] values;
	
	Point LocationBox = new Point(0,0);
	Dimension PaneSize = new Dimension(600,150); 
	JLabel jl=new JLabel();
	
	JButton SourcePort;
	JButton DestinationPort;
	JButton SeqNumber;
	JButton AckNumber;
	JButton HLen;
	JButton Reserved;
	JButton Flags;
	JButton WindowSize;
	JButton Checksum;
	JButton UrgentPointer;
	
	
	public TCPbit(){
		
		SourcePort = new JButton("Socure Port");
		DestinationPort = new JButton("Destination Port");
		SeqNumber = new JButton("Sequence Number");
		AckNumber = new JButton("Acknowledgment Number");
		HLen = new JButton("Header Length");
		Reserved = new JButton("Reserved");
		Flags = new JButton("Flags");
		WindowSize = new JButton("Window Size");
		Checksum = new JButton("Checksum");
		UrgentPointer = new JButton("Urgent Pointer");


		this.setLayout(null);
		this.setBackground(Color.white);

		
		
		this.removeBoxBorder(SourcePort);
		this.removeBoxBorder(DestinationPort);
		this.removeBoxBorder(SeqNumber);
		this.removeBoxBorder(AckNumber);
		this.removeBoxBorder(HLen);
		this.removeBoxBorder(Reserved);
		this.removeBoxBorder(Flags);
		this.removeBoxBorder(WindowSize);
		this.removeBoxBorder(Checksum);
		this.removeBoxBorder(UrgentPointer);
		/*666600
		666633
		999900
		999933
		CCCC00
		CCCC33*/
		SourcePort.setBackground(new Color(0x66,0x66,0x00));
		DestinationPort.setBackground(new Color(0x66,0x66,0x00));
		
		SeqNumber.setBackground(new Color(0x80,0x80,0x00));
		
		AckNumber.setBackground(new Color(0x99,0x99,0x00));
		
		HLen.setBackground(new Color(0x99,0x99,0x33));
		Reserved.setBackground(new Color(0x99,0x99,0x33));
		Flags.setBackground(new Color(0x99,0x99,0x33));
		WindowSize.setBackground(new Color(0x99,0x99,0x33));
		
		Checksum.setBackground(new Color(0xcc,0xcc,0x00));
		UrgentPointer.setBackground(new Color(0xcc,0xcc,0x00));
		this.add(SourcePort);
		this.add(DestinationPort);
		this.add(SeqNumber);
		this.add(AckNumber);
		this.add(HLen);
		this.add(Reserved);
		this.add(Flags);
		this.add(WindowSize);
		this.add(Checksum);
		this.add(UrgentPointer);
		jl.setBounds(0, 160, 600, 120);
		this.add(jl);
	}
	public static void removeBoxBorder(JButton box){
		box.setBorderPainted(true);
		box.setContentAreaFilled(true);
		box.setDefaultCapable(false);
		box.setFocusPainted(false);
	}

	
	public void getInfos(final String[] names, final Object[] values) {
		
		this.names = names;
		this.values = values;
		Font f = new Font("",Font.BOLD,13);
		jl.setFont(f);
		
		SourcePort.setBounds(LocationBox.x, LocationBox.y,PaneSize.width/2, PaneSize.height/5);
		SourcePort.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				
				jl.setText((names[0]+" : "+values[0]));
			}
		});
		
		DestinationPort.setBounds(LocationBox.x + SourcePort.getWidth(), LocationBox.y,PaneSize.width/2, PaneSize.height/5);
		DestinationPort.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[1]+" : "+values[1]);
			}
		});
		SeqNumber.setBounds(LocationBox.x, LocationBox.y+SourcePort.getHeight(),PaneSize.width, PaneSize.height/5);
		SeqNumber.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[2]+" : "+values[2]);
			}
		});
		
		AckNumber.setBounds(LocationBox.x, SeqNumber.getY()+SeqNumber.getHeight(),PaneSize.width, PaneSize.height/5);
		AckNumber.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[3]+" : "+values[3]);
			}
		});
		
		HLen.setBounds(LocationBox.x, AckNumber.getY()+AckNumber.getHeight(),PaneSize.width/6, PaneSize.height/5);
		HLen.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText("nothing");
			}
		});
		
		Reserved.setBounds(LocationBox.x+HLen.getWidth(), AckNumber.getY()+AckNumber.getHeight(),PaneSize.width/6, PaneSize.height/5);
	/*	AckNumber.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[2]+" : "+values[2]);
			}
		});*/
		
		Flags.setBounds(Reserved.getX()+Reserved.getWidth(),AckNumber.getY()+AckNumber.getHeight(),PaneSize.width/6, PaneSize.height/5);
		Flags.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				StringBuffer sb = new StringBuffer();
				for(int i=4;i<=9;i++){
					sb.append(names[i]+" : "+values[i]+"<br></br>");
				}
				jl.setText("<html>"+sb.toString()+"</html>");
				
			}
		});
		
		WindowSize.setBounds(Flags.getX()+Flags.getWidth(), AckNumber.getY()+AckNumber.getHeight(),PaneSize.width/2, PaneSize.height/5);
		WindowSize.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[10]+" : "+values[10]);
			}
		});
		
		Checksum.setBounds(LocationBox.x, HLen.getY()+HLen.getHeight(),PaneSize.width/2, PaneSize.height/5);
		Checksum.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText("nothing");
			}
		});
		
		UrgentPointer.setBounds(LocationBox.x+Checksum.getWidth(), HLen.getY()+HLen.getHeight(),PaneSize.width/2, PaneSize.height/5);
		UrgentPointer.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText("nothing");
			}
		});
		
	}


	public String isProtocol() {
		// TODO Auto-generated method stub
		return "TCP";
	}


	public void onPane() {
		// TODO Auto-generated method stub
		this.setVisible(true);
	}


	public void offPane() {
		// TODO Auto-generated method stub
		this.setVisible(false);
	}
	

}
